<template>
  <div class="game" ref="game">
    <div ref="skyDom" @click="bounce" class="sky"></div>
    <div ref="landDom" class="land"></div>
    <div ref="birdDom" class="bird swing1"></div>

  </div>
  <Tip></Tip>
</template>

<script setup>
import { Sky } from '../hooks/Sky'
import { Land } from '../hooks/Land'
import { Bird } from '../hooks/Bird'
import { PairList } from '../hooks/Pair'
import Tip from './Tip.vue'
import { onMounted, ref } from 'vue'
const game = ref(null)
const skyDom = ref(null)
const landDom = ref(null)
const birdDom = ref(null)
let sky = null
let land = null
let bird = null
let pairList = null
/**
 * 点击跳起来
 */
document.addEventListener('keyup', function () {
  bird.bounce()
  // bird.endwith()
})
/**
 * 小鸟飞跃的回调
 */
function bounce() {
  bird.bounce()
}
onMounted(() => {
  sky = new Sky(skyDom.value)
  land = new Land(landDom.value)
  bird = new Bird(birdDom.value)
  pairList = new PairList(-100, game.value)
  pairList.startProducter()
  bird.startwith()
  let step = () => {
    sky.move(16 / 1000)
    land.move(16 / 1000)
    bird.move(16 / 1000)
    pairList.pairs.forEach(item => {
      item.move(16 / 1000)
    })
    requestAnimationFrame(step)
  }
  requestAnimationFrame(step)
})
</script>

<style lang="scss">
.game {
  border: 1px solid #ff3d3d;
  width: 800px;
  height: 600px;
  position: relative;
  overflow: hidden;

  ::after {
    content: '';
    display: block;
    clear: both;
  }

  & .sky {
    position: absolute;
    top: 0;
    left: 0;
    width: 200%;
    height: 600px;
    background: url(../assets/img/sky.png);
  }

  & .land {
    position: absolute;
    top: 488px;
    left: 0;
    width: 200%;
    height: 112px;
    background: url(../assets/img/land.png);
  }

  & .bird {
    background: url(../assets/img/bird.png);
    position: absolute;
    width: 33px;
    height: 26px;
    left: 150px;
    top: 150px;
  }
}

.bird.swing1 {
  background-position: -8px -10px;
}

.bird.swing2 {
  background-position: -60px -10px;
}

.bird.swing3 {
  background-position: -113px -10px;
}

.pipe {
  position: absolute;
  height: 100px;
  width: 52px;
  left: 500px;
}

.pipe.up {
  background: url(../assets/img/pipeDown.png);
  top: 0;
  background-position: bottom;
}

.pipe.down {
  background: url(../assets/img/pipeUp.png);
  bottom: 112px;
}

.info {
  text-align: center;
}
</style>